﻿var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";
var upWindow = window.parent.frames["zwIframe"];

function fy_dialog_close() {
//	window.parent.$(".mask").remove();
//	window.parent.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");
    window.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");

}

function getUpFields(name) {
    return upWindow.$("[validate*='name:\"" + name + "\"']");
}
function getUpFieldsByFieldval(name) {
    return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
}
function getUpAccountFieldValue(name) {
    var field = getUpFields(name).eq(0);
    return {
        displayValue: field.is("span") ? field.text() : field.val(),
        value: eval("(" + field.parent("span").attr("fieldVal") + ")").value
    };
}

function getUpFieldValueByName(name, index) {
    return getUpFieldValue(getUpFieldsByFieldval(name).eq(index ? index : 0));
}

function getUpFieldValue(field) {
    var fieldVal = field.attr("fieldVal");
    var editTag = field.hasClass(editClass);
    var browseTag = field.hasClass(browseClass);
    var addTag = field.hasClass(addClass);
    if (fieldVal != null && typeof (fieldVal) != 'undefined') {
        fieldVal = eval("(" + fieldVal + ")");
    } else {
        return null;
    }
    var obj = new Object();
    obj.displayName = fieldVal.displayName;
    obj.name = fieldVal.name;
    var tag = true;
    switch (fieldVal.inputType) {
        case "text":
        case "textarea":

            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "checkbox":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "radio":
            if (editTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            }
            break;
        case "select":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + " option:selected")
                        .attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).attr("val4cal");
            }
            break;
        case "date":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
                if (obj.value != "") {
                    obj.value = obj.value + " 00:00";
                }
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "datetime":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "flowdealoption":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "lable":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relationform":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relation":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "project":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "member":
        case "multimember":
        case "account":
        case "multiaccount":
        case "department":
        case "multidepartment":
        case "post":
        case "multipost":
        case "level":
        case "multilevel":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + "_txt").val();
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).text();
            }
            break;
        case "attachment":
        case "image":
        case "document":
            if (editTag || browseTag) {
                var attNames = "";
                var tempField = field.find("#" + fieldVal.name);
                var tempFileId = "";
                if (tempField[0].tagName.toLowerCase() == "input") {
                    tempFileId = tempField.val();
                } else if (tempField[0].tagName.toLowerCase() == "span") {
                    tempFileId = tempField[0].innerHTML;
                }
                var atts = upWindow.getAttBySubreference(tempFileId);
                for (var j = 0; j < atts.length; j++) {
                    attNames += atts[j].filename + ",";
                }
                if (attNames != "") {
                    attNames = attNames.substr(0, attNames.length - 1);
                }
                obj.value = attNames;
            }
            break;
        case "outwrite":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "externalwrite-ahead":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "exchangetask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "querytask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        default:
            tag = false;
    }
    if (obj.value != undefined) {
        obj.value = obj.value.replace("\r\n", ";");
        obj.value = obj.value.replace("\n", ";");
    }
    return obj;
}
function setUpFieldValue(name, value) {
    upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input").val(
            value);
    upWindow.$("[fieldval*='displayName:\"" + name + "\"']").find("input")
            .trigger("blur");
}

function setUpFieldValue2(field, value, displayValue, index) {

    try {

        var fieldVal = field.attr("fieldVal");
        var editTag = field.hasClass(editClass);
        var addTag = field.hasClass(addClass);
        value = (value);
        displayValue = displayValue ? (displayValue) : displayValue;
        if (fieldVal != null && typeof (fieldVal) != 'undefined') {
            fieldVal = eval("(" + fieldVal + ")");
        } else {
            return null;
        }
        var tag = true;
        switch (fieldVal.inputType) {
            case "text":
            case "textarea":
                if (editTag) {
                    var inField = field.find("#" + fieldVal.name);
                    inField.val(value);
                    inField.trigger("blur");
                } else if (addTag) {
                    var inField = field.find("#" + fieldVal.name);
                    inField.val(value);
                    inField.trigger("blur");
                }
                break;
            case "checkbox":
                if (editTag) {
                    field.find("#" + fieldVal.name).attr("checked", true);
                    field.find("#" + fieldVal.name).trigger("click");
                }
                break;
            case "radio":
                if (editTag) {
                    field.find(":radio").eq(index).attr("checked", true);
                    field.find(":radio").eq(index).trigger("click");
                }
                break;
            case "select":
                if (editTag) {
                    var options = field.find("#" + fieldVal.name + " option");
                    for (var k = 0; k < options.length; k++) {
                        if (options.eq(k).text() == value)
                            options.eq(k).attr("selected", true);
                    }
                    field.find("#" + fieldVal.name).trigger("change");
                }
                break;
            case "date":
                if (editTag) {
                    field.find("#" + fieldVal.name).val(value);
                    field.find("#" + fieldVal.name).trigger("blur");
                }
                break;
            case "datetime":
                if (editTag) {
                    field.find("#" + fieldVal.name).val(value);
                    field.find("#" + fieldVal.name).trigger("blur");
                }
                break;
            case "member":
            case "multimember":
            case "account":
            case "multiaccount":
            case "department":
            case "multidepartment":
            case "post":
            case "multipost":
            case "level":
            case "multilevel":
                if (editTag) {
                    alert(editTag);
                    field.find("#" + fieldVal.name + "_txt").val(value);
                }
                break;
        }
    } catch (e) {
        // TODO: handle exception
    }
}

var contains_BM = false;
var contains_BG10001G = false;
var contains_AB = false;
var contains_AP = false;

var corpMap = {
    // "福耀玻璃工业集团股份有限公司" : "FYG,FYA",
    "集团总办": "FYG,FYA,SKG,SKA,SJG,SJA",
    "福建省万达汽车玻璃工业有限公司": "FYG,FYA",
    "福耀(福建)巴士玻璃有限公司": "FYG,FYA",
    "广州福耀玻璃有限公司": "GZG,GZA",
    "福耀集团(上海)汽车玻璃有限公司": "SJG,SJA",
    "上海福耀客车玻璃有限公司": "SKG,SKA",
    "福耀玻璃(湖北)有限公司": "HBG,HBA",
    "福耀玻璃(重庆)有限公司": "XQA,XQG",
    "重庆万盛福耀玻璃有限公司": "CQG,CQA",
    "福耀集团北京福通安全玻璃有限公司": "BJG,BJA",
    "福耀集团长春有限公司": "CJG,CJA",
    "福耀(长春)巴士玻璃有限公司": "CKG,CKA",
    "郑州福耀玻璃有限公司": "ZZG,ZZA",
    "福耀集团(沈阳)汽车玻璃有限公司": "SYG,SYA",
	"天津泓德汽车玻璃有限公司":"TF,TFG"
};
var item_flMap = {
    "A类": 1,
    "B类": 2,
    "C类": 4,
    "D类": 5,
    "E类": 3,
    "F类": 6
}

var fields = {
    head: {
        item_fl: "材料类别",
        req_dep: "申请部门",
        req_date: "申请时间",
        req_user: "申请人",
        req_num: "ERP单据编号",
        req_status: "申请状态"
                // ,
                // head_memo : "备注"
    },
    body: {
        LINE_NUM: "行号",
        ITEM_NUM: "材料编码",
        ITEM_DES: "材料说明",
        REQ_QTY: "申请数量",
        ITEM_UOM: "单位",
        delv_qty_total: "时限内累计领料量",
        attribute2: "定额",
        alias_name: "账户别名",
        req_memo: "申请备注",
        to_subinv_code: "工厂子库存",
        sub_inventory: "仓库子库存"
    }
};

/**
 * 获取TR的上级节点
 */
var getTRParentNode = function (node) {
    if (node && node.tagName && node.tagName.toUpperCase() != "TR") {
        return getTRParentNode(node.parentNode);
    } else {
        return node.parentNode;
    }
};



function query() {

    var number = document.getElementById("number").value;
    var org_id = corpMap[getUpFieldValue(getUpFieldsByFieldval("单位选择器").eq(0)).value];
	alert(getUpFieldsByFieldval("单位选择器").eq(0));
	alert(getUpFieldValue(getUpFieldsByFieldval("单位选择器").eq(0)).value +"\n重庆万盛福耀玻璃有限公司" );
	
    if (!org_id || org_id == "") {
        //alert("用户当前单位没有允许使用当前表单，请您联系OA系统管理员。");
		alert("org_id")
        return false;
    }
    if (!number || number == "" || isNaN(number)) {
        alert("ERP单据编号不能为空或非数字类型");
        return false;
    }
    var inputs = document.getElementsByTagName("input");

    for (var i = 0; i < inputs.length; i++) {
        inputs[i].disabled = true;
    }
    var xmlhttp = null;
    if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
        xmlhttp = new XMLHttpRequest();
    } else {// code for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) {
            if (xmlhttp.status == 200) {
                text = xmlhttp.responseText;

                if (text == 1) {
                    alert("ERP单据编号不能为空");
                } else if (text == 2) {
                    alert("未查找到对应的单据");
                } else if (text == 3) {
                    alert("访问ERP数据库异常");
                } else if (text.indexOf("4:") == 0) {
                    text = text.replace("4:", "");
                    alert(text == "null" ? "未查询到单据编号对应的数据" : text);
                } else if (text != "") {
                    var salesContract = eval("(" + text + ")");

                    for (var fieldKey in fields.head) {
                        setUpFieldValue(
                                fields.head[fieldKey],
                                (salesContract[fieldKey] ? salesContract[fieldKey]
                                        : ""));
                    }

                    if (getUpFieldsByFieldval("分类_值").length > 0)
                        setUpFieldValue("分类_值",
                                item_flMap[salesContract.item_fl]);
                    var lines = eval("([" + salesContract.lines + "])");
                    if (lines) {
                        var lineSize = lines.length;
                        var oldlength = getUpFieldsByFieldval("行号").length;
                        // var trParentNode = getTRParentNode();
                        if (lineSize != oldlength) {
                            var tbody = getUpFieldsByFieldval("行号").eq(0)
                                    .parent();
                            while (!tbody.is("tbody")) {
                                tbody = tbody.parent();
                            }
                            for (var i = 0, length = Math.abs(lineSize
                                    - oldlength); i < length; i++) {
                                if (lineSize > oldlength) {
                                    upWindow.fy_addEmpty(tbody.children()
                                            .last());
                                } else {
                                    upWindow.fy_delCurrentRepeat(tbody
                                            .children().last());
                                }
                            }
                        }
                        for (var key in fields.body) {
                            setUpFieldValue(fields.body[key], "");
                        }

                        var overtake = false;
                        for (var i = 0; i < lineSize; i++) {
                            // delv_qty_total : "时限内累计领料量",
                            // attribute2 : "定额"
                            if (!isNaN(lines[i].attribute2)
                                    && lines[i].attribute2 != '0'
                                    && (!isNaN(lines[i].delv_qty_total) ? lines[i].delv_qty_total
                                            : 0)
                                    - lines[i].attribute2 > 0) {
                                overtake = true;
                            }
                            for (var bodyKey in fields.body) {
                                if ("REQ_QTY,attribute2,delv_qty_total"
                                        .indexOf(bodyKey) == -1) {
                                    setUpFieldValue2(
                                            getUpFieldsByFieldval(
                                                    fields.body[bodyKey]).eq(i),
                                            (lines[i][bodyKey] ? lines[i][bodyKey]
                                                    : ""));
                                } else {
                                    setUpFieldValue2(
                                            getUpFieldsByFieldval(
                                                    fields.body[bodyKey]).eq(i),
                                            (lines[i][bodyKey] ? (lines[i][bodyKey]
                                                    .indexOf(".") == 0 ? "0"
                                                    + lines[i][bodyKey]
                                                    : lines[i][bodyKey])
                                                    : ""));
                                }

                                if ("ITEM_NUM" == bodyKey) {
                                    if (lines[i][bodyKey] == "BG10001G") {
                                        contains_BG10001G = true;
                                    } else if (lines[i][bodyKey].indexOf("BM") == 0) {
                                        contains_BM = true;
                                    } else if (lines[i][bodyKey].indexOf("AB") == 0) {
                                        contains_AB = true;
                                    } else if (lines[i][bodyKey].indexOf("AP") == 0) {
                                        contains_AP = true;
                                    }
                                }
                            }
                        }
                        setUpFieldValue2(getUpFieldsByFieldval("是否超定额").eq(0),
                                overtake, null, 0);
                        setUpFieldValue2(getUpFieldsByFieldval("是否超定额").eq(0),
                                overtake, null, 1);
                    }
                    // pdoc.getElementsByName("申请数量")[0].focus();
                    var amountNodes = getUpFieldsByFieldval("申请数量");
                    var xiaoji_shenqingshuliang = 0;
                    var xiaoji_lingliaoliang = 0;
                    for (var index = 0, amoutNodesLength = amountNodes.length; index < amoutNodesLength; index++) {
                        if (!isNaN(amountNodes[index].value)) {
                            xiaoji_shenqingshuliang += parseFloat(getUpFieldValue(amountNodes
                                    .eq(index)).value);
                        }
                        if (!isNaN(amountNodes[index].value)) {
                            xiaoji_lingliaoliang += parseFloat(getUpFieldValue(getUpFieldsByFieldval(
                                    "时限内累计领料量").eq(index)).value);
                        }
                    }

                    if (getUpFieldsByFieldval("审批节点选择").length > 0) {
                        setUpFieldValue2(getUpFieldsByFieldval("审批节点选择").eq(0),
                                (contains_BM ? 3 : (contains_BG10001G ? 2 : 1)));
                    }

                    if (getUpFieldsByFieldval("分类_值").length > 0
                            && getUpFieldValue(getUpFieldsByFieldval("分类_值")
                                    .eq(0)).value == 1) {
                        setUpFieldValue("分类_值",
                                (contains_AB && contains_AP ? 13
                                        : contains_AB ? 11 : 12));
                    }

                    fy_dialog_close();
                    return;
                } else {
                    alert("获取数据发生异常");
                }
                for (var i = 0; i < inputs.length; i++) {
                    inputs[i].disabled = false;
                }
            } else {
                alert("连接服务器异常");
            }
        }
    };
    xmlhttp.open("get",
            "../../erp2oaController.do?method=queryLingLiaoFaLiao&number="
            + encodeURIComponent(number) + "&org_id=" + org_id
            + "&temp=" + Math.random(), true);
    xmlhttp.send();

    return false;
}
